﻿namespace Sunxsh.Es.Common;

public static class AssertEx
{
	/// <summary>
	/// 断言为真
	/// </summary>
	public static void IsTrue(bool value, string message)
	{
		if (!value)
		{
			throw new InvalidOperationException(message);
		}
	}

	public static void HasValue(string value, string name)
	{
		if (string.IsNullOrEmpty(value))
		{
			throw new InvalidOperationException($"'{name}' is null ");
		}
	}

	public static void HasValue<T>(T value, string name)
	{
		if (Equals(value, null) || Equals(value, default(T)))
		{
			throw new InvalidOperationException($"'{name}' is null ");
		}
	}

	/// <summary>
	/// 断言不能为空
	/// </summary>
	public static void IsNotNull(object value, string name)
	{
		if (value == null)
		{
			throw new InvalidOperationException($"'{name}' 不能为空");
		}
	}

	/// <summary>
	/// 断言不存在
	/// </summary>
	public static void IsNotExists(object value, string name)
	{
		if (value == null)
		{
			throw new InvalidOperationException($"'{name}' is not exists.");
		}
	}

	public static void IsNotNull(string value, string name)
	{
		if (string.IsNullOrWhiteSpace(value))
		{
			throw new InvalidOperationException($"'{name}' 不能为空");
		}
	}

	/// <summary>
	/// 断言相等
	/// </summary>
	public static void ShouldBe<T>(T source, T targetValue, string name)
	{
		if (!Equals(source, targetValue))
		{
			throw new InvalidOperationException($"'{name}' should be {targetValue}. but value is {source}");
		}
	}
}